<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class FreightTemplate extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_freight_template';



    protected $append = [

        'config'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-07-11 17:07
     * @功能说明:
     */
    public function getConfigAttr($value,$data){

        if(!empty($data['id'])){

            $config_model = new FreightConfig();

            $list = $config_model->where(['template_id'=>$data['id']])->select()->toArray();

            return $list;
        }


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @param $data
     * @功能说明:添加模版
     * @author chenniang
     * @DataTime: 2022-07-11 16:32
     */
    public function tmplAdd($data){

        if(isset($data['config'])){

            $config = $data['config'];


            unset($data['config']);

        }

        $res = $this->dataAdd($data);

        $id = $this->getLastInsID();

        if(isset($config)){

            $this->updateSome($config,$id,$data['uniacid']);
        }

        return $id;
    }


    /**
     * @param $dis
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-11 16:37
     */
    public function tmplUpdate($dis,$data){

        if(isset($data['config'])){

            $config = $data['config'];

            unset($data['config']);

        }

        $res = $this->dataUpdate($dis,$data);

        if(isset($config)){

            $this->updateSome($config,$dis['id'],$data['uniacid']);
        }

        return $res;

    }


    /**
     * @param $data
     * @param $id
     * @param $uniacid
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-11 16:11
     */
    public function updateSome($data,$id,$uniacid){

        $config_model   = new FreightConfig();

        $province_model = new FreightProvince();

        $config_model->where(['template_id'=>$id])->delete();

        $province_model->where(['template_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $key=>$value){

                $value['uniacid']     = $uniacid;

                $value['template_id'] = $id;

                $province = $value['province'];

                unset($value['province']);

                $config_model->dataAdd($value);

                $config_id = $config_model->getLastInsID();

                foreach ($province as $ks=>$vs){

                    $insert[$ks] = [

                        'uniacid' => $uniacid,

                        'config_id'=> $config_id,

                        'province' => $vs,

                        'template_id' => $id

                    ];
                }

                $province_model->saveAll($insert);
            }
        }

        return true;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}